<?php
/**
 * CEOfox
 *
 * @copyright  Copyright 2011-2012 CEOfox, Inc.
 * @license    http://ceofox.com/pages/license
 * @version    $Id: index.class.php 57089 2013-05-24 09:40:17 $
 * @author     CEOfox, Inc.
 */
?>
<?php

defined('PHPFOX') or exit('NO DICE!');
define('PHPFOX_FEED_FROM_FOLLOWING', TRUE);
class Follow_Component_Controller_Index extends Phpfox_Component
{
    public function process()
    {
        $categories = Phpfox::getService('marketplace.category')->getForBrowse();
		foreach($categories as $key=>$cat){
			reset($arrEx);
			$endName = '';
			$arrEx = array();
			if($cat['name'][0]=='{'){
				$arrEx = explode("var=",$cat['name']);
				$endName = substr($arrEx[1], 6, -7);
				$categories[$key]['name'] = Phpfox::getPhrase($endName);
			}
		}
		$this->template()->assign(array(
					'categories' => $categories)
		);
        Phpfox::isUser(true);
        $bIsUser = true;
        if($aParentModule = $this->getParam('aParentModule'))
        {
            
            if(!empty($aParentModulep['item_id']))
            {
                $bIsUser = false;
            }
            if($aParentModule['module_id'] == 'pages')
            {
                $this->template()->assign(array(
                    'aParentModule' => $aParentModule,
                ));    
                return Phpfox::getComponent('follow.pages', array('bNoTemplate' => true), 'controller');
            }
        }

        
        
        $bCanFollow = false;
        $bIsCurrentUser = false;
        $aUser = $this->getParam('aUser');
        $bIsLogin = ( Phpfox::getUserId() > 0 )? true : false;
        $oFollow = Phpfox::getService('follow');
        if(!isset($aUser) || ($aUser['user_id'] == phpfox::getUserId()))
        {
            $aUser['user_id'] = phpfox::getUserId();
            $bIsCurrentUser = true;
        }
        if($aUser['user_id'] != phpfox::getUserId())
        {
            if($oFollow->canFollow($aUser['user_id']))
            {
                $bCanFollow = true;
            }
        }
        if(!isset($aParentModule))
        {
            $aParentModule = array(
                'module_id' => 'user',
                'item_id' => $aUser['user_id'],
            );
        }
        if($aVals = $this->request()->getArray('val'))
          {
              if($oFollow->setFilter(Phpfox::getUserId(), $aUser['user_id'], 'user', $aVals))
              {
                  $this->url()->send('current',null, Phpfox::getPhrase('follow.update_setting_successfully'));
              }
              else
              {
                  $this->url()->send('current',null, Phpfox::getPhrase('follow.there_are_some_errors_happen_when_perform_this_action'));
              }
          }
        if($bIsCurrentUser)
        {
            $aCustom_filters  = array(
            Phpfox::getPhrase('follow.searchby') => array(
                'param' => 'search-by',
                'default_phrase' => Phpfox::getPhrase('follow.following'),
                'data' => array(
                    array(
                        'link' => 'following',
                        'phrase' => Phpfox::getPhrase('follow.following')
                    ),
                    array(
                        'link' => 'follower',
                        'phrase' => Phpfox::getPhrase('follow.follower')
                    ),
                    array(
                        'link' => 'blocker',
                        'phrase' => Phpfox::getPhrase('follow.blocker')
                    )
            )));
        }
        else
        {
            $aCustom_filters  = array
            (
                Phpfox::getPhrase('follow.searchby') => array(
                    'param' => 'search-by',
                    'default_phrase' => Phpfox::getPhrase('follow.following'),
                    'data' => array(
                            array(
                                'link' => 'following',
                                'phrase' => Phpfox::getPhrase('follow.following')
                            ),
                            array(
                                'link' => 'follower',
                                'phrase' => Phpfox::getPhrase('follow.follower')
                            )
                        )
            ));
            $aFilter = Phpfox::getService('follow')->getFilter(Phpfox::getUserId(), $aUser['user_id'], 'user');
            
            $this->template()->assign(array(
               'aFilter' => $aFilter 
            ));
        }
        
        $aCustom_filters[Phpfox::getPhrase('follow.feeds_of')]  = array
        (
           
                'param' => 'fof',
                'default_phrase' => Phpfox::getPhrase('follow.all'),
                'data' => array
                (
                        array
                        (
                            'link' => 'all',
                            'phrase' => Phpfox::getPhrase('follow.all')
                        ),
                        array
                        (
                            'link' => 'user',
                            'phrase' => Phpfox::getPhrase('follow.user')
                        ),
                        array
                        (
                            'link' => 'pages',
                            'phrase' => Phpfox::getPhrase('follow.page')
                        )
                )
            
        );
        ksort($aCustom_filters);
        Phpfox::getservice('follow')->checkBlockUser($aUser['user_id']);
        $this->search()->set(array(
                'type' => 'follow',
                'field' => 'follow.user_id',                
                'search_tool' => array(
                    'table_alias' => 'follow',
                    'search' => array(
                        'action' => '',
                        //'action' => $this->url()->makeUrl('follow', array('view' => $this->request()->get('view'))),
                        'default_value' => Phpfox::getPhrase('follow.search_follow_dot'),
                        'name' => 'search',
                        'field' => array('follow.user_id', 'follow.item_id')
                    ),
                    'sort' => array(
                        'latest' => array('follow.time_stamp', 'Lastest'),
                    ),
                    'show' => array(5, 10, 15),
                    'custom_filters' => $aCustom_filters
                )
        ));

        $aBrowseParams = array(
            'module_id' => 'follow',
            'alias' => 'follow',
            'field' => 'user_id',
            'table' => Phpfox::getT('follow')
        );
        $sFeedOf = $this->request()->get('fof','');
        
        if($sKeyword = $this->search()->get('search'))
        {
            $iAmount = ($this->request()->getInt('show'))? $this->request()->get('show'): 5;
            $iOffset = ($this->request()->getInt('page') >= 2)?($this->request()->getInt('page') - 1)*$iAmount:0;
            switch($this->request()->get('search-by'))
            {
                case "follower":
                    $aIndex_Followers = phpfox::getService('follow')->searchFollower($sKeyword, $aUser['user_id'], $iOffset, $iAmount);                
                    $iCnt = phpfox::getService('follow')->countsearchFollower($sKeyword, $aUser['user_id']);
                    if($aIndex_Followers)
                    {
                        foreach($aIndex_Followers as $iKey => $aIndex_Follower)
                        {
                            if($aIndex_Follower['user_id'] == phpfox::getUserId())
                            {
                                $aIndex_Followers[$iKey]['canFollow'] = 0;
                                $aIndex_Followers[$iKey]['isFollow'] = 0;
                            }
                            else
                            {
                                if(Phpfox::getService('follow')->canFollow($aIndex_Follower['user_id']))
                                {
                                    if(phpfox::getService('follow')->isFollow(Phpfox::getUserId(), $aIndex_Follower['user_id'], 'user'))
                                    {
                                        $aIndex_Followers[$iKey]['isFollow'] = 0;
                                    }
                                    else
                                    {
                                        $aIndex_Followers[$iKey]['isFollow'] = 1;
                                    }
                                    $aIndex_Followers[$iKey]['canFollow'] = 1;
                                }
                                else
                                {
                                    $aIndex_Followers[$iKey]['isFollow'] = 0;
                                    $aIndex_Followers[$iKey]['canFollow'] = 0;
                                }
                            }
                        }
                    }
                    
                    $iAmountResult = count($aIndex_Followers);
                    break;
                case "":
                case "following":
                    $aIndex_Followings = phpfox::getService('follow')->searchFollowing($sKeyword, $aUser['user_id'], $iOffset, $iAmount);
                    if($aIndex_Followings)
                    {
                        foreach($aIndex_Followings as $iKey => $aIndex_Following)
                        {
                            if($aIndex_Following['user_id'] == phpfox::getUserId())
                            {
                                $aIndex_Followings[$iKey]['canFollow'] = 0;
                                $aIndex_Followings[$iKey]['isFollow'] = 0;
                            }
                            else
                            {
                                if(Phpfox::getService('follow')->canFollow($aIndex_Following['user_id']))
                                {
                                    if(phpfox::getService('follow')->isFollow(Phpfox::getUserId(), $aIndex_Following['user_id'], 'user'))
                                    {
                                        $aIndex_Followings[$iKey]['isFollow'] = 0;
                                    }
                                    else
                                    {
                                        $aIndex_Followings[$iKey]['isFollow'] = 1;
                                    }
                                    $aIndex_Followings[$iKey]['canFollow'] = 1;
                                }
                                else
                                {
                                    $aIndex_Followings[$iKey]['isFollow'] = 0;
                                    $aIndex_Followings[$iKey]['canFollow'] = 0;
                                }
                            }
                        }
                    }
                    //d($aIndex_Followings);
                    $iCnt = phpfox::getService('follow')->countsearchFollowing($sKeyword, $aUser['user_id']);
                    $iAmountResult = count($aIndex_Followings);
                    break;
                case "blocker":
                    if($bIsCurrentUser)
                    {
                        $aIndex_Blockers = phpfox::getService('follow')->searchBlocker($sKeyword, $aUser['user_id'], $iOffset, $iAmount);
                        $iCnt = phpfox::getService('follow')->countsearchBlocker($sKeyword, $aUser['user_id']);
                        $iAmountResult = count($aIndex_Blockers);
                        break;
                    }
            }
        }
        Phpfox::getLib('pager')->set(array('page' => $this->search()->getPage(),
         'size' => $this->search()->getDisplay(), 'count' => isset($iCnt)? $iCnt: 0));




        $bIsFollow = phpfox::getservice('follow')->isFollow(Phpfox::getUserId(),$aUser['user_id'], 'user');
        if($bIsFollow)
        {
        

        $iUserId = $this->getParam('aUser');
        $bIsCustomFeedView = false;
        $sCustomViewType = null;        
        
        if (PHPFOX_IS_AJAX && ($iUserId = $this->request()->get('profile_user_id')))
        {
            if (!defined('PHPFOX_IS_USER_PROFILE'))
            {
                define('PHPFOX_IS_USER_PROFILE', true);
            }
            $aUser = Phpfox::getService('user')->get($iUserId);
            
            $this->template()->assign(array(
                    'aUser' => $aUser
                )
            );
        }    
        
        if (PHPFOX_IS_AJAX && $this->request()->get('callback_module_id'))
        {
            $aCallback = Phpfox::callback($this->request()->get('callback_module_id') . '.getFeedDisplay', $this->request()->get('callback_item_id'));
            $this->setParam('aFeedCallback', $aCallback);
        }
        
        $aFeedCallback = $this->getParam('aFeedCallback', null);
        
        if(phpfox::getLib('module')->getModuleName() == 'follow')
        {
            $aFeedCallback['disable_share'] = true;
        }
        
        $bIsProfile = (is_numeric($iUserId) && $iUserId > 0);
        
        if ($this->request()->get('feed') && $bIsProfile)
        {
            switch ($this->request()->get('flike'))
            {
                default:
                    if ($sPlugin = Phpfox_Plugin::get('follow.component_block_display_process_flike'))
                    {
                        eval($sPlugin);
                    }                    
                    break;
            }
        }
              
        $iFeedPage = $this->request()->get('page', 0);
        
        if ($this->request()->getInt('status-id') 
            || $this->request()->getInt('comment-id') 
            || $this->request()->getInt('link-id')
            || $this->request()->getInt('plink-id')
            || $this->request()->getInt('poke-id')
            || $this->request()->getInt('feed')
        )
        {
            $bIsCustomFeedView = true;
            if ($this->request()->getInt('status-id'))
            {
                $sCustomViewType = Phpfox::getPhrase('feed.status_update_iid',array('iId' => $this->request()->getInt('status-id')));
            }
            elseif ($this->request()->getInt('link-id'))
            {
                $sCustomViewType = Phpfox::getPhrase('feed.link_iid', array('iId' => $this->request()->getInt('link-id')));
            }
            elseif ($this->request()->getInt('plink-id'))
            {
                $sCustomViewType = Phpfox::getPhrase('feed.link_iid', array('iId' => $this->request()->getInt('plink-id')));
            }            
            elseif ($this->request()->getInt('poke-id'))
            {
                $sCustomViewType = Phpfox::getPhrase('feed.poke_iid',array('iId' =>$this->request()->getInt('poke-id')));
            }            
            elseif ($this->request()->getInt('comment-id'))
            {
                $sCustomViewType = Phpfox::getPhrase('feed.wall_comment_iid',array('iId' => $this->request()->getInt('comment-id')));                        
                
                Phpfox::getService('notification.process')->delete('feed_comment_profile', $this->request()->getInt('comment-id'), Phpfox::getUserId());
            }
            elseif ($this->request()->getInt('feed'))
            {
                $sCustomViewType = Phpfox::getPhrase('feed.feed');
            }
        }

        if ((!isset($aFeedCallback['item_id']) || $aFeedCallback['item_id'] == 0))
        {
            $aFeedCallback['item_id'] = ((int)$this->request()->get('amp;callback_item_id')) > 0 ? $this->request()->get('amp;callback_item_id') : $this->request()->get('callback_item_id');
        }
        
        if(isset($sFeedOf) && (empty($sFeedOf) || (!empty($sFeedOf) && $sFeedOf == 'all')))
        {
            $aRows = Phpfox::getService('follow.feed')->callback($aFeedCallback)->get($aUser['user_id'], ($this->request()->get('feed') ? $this->request()->get('feed') : null), $iFeedPage);
        }
        else
        {
            $aRows = Phpfox::getService('follow.feed')->callback($aFeedCallback)->get($aUser['user_id'], ($this->request()->get('feed') ? $this->request()->get('feed') : null), $iFeedPage, false, $sFeedOf);
        }
        
        if ($bIsCustomFeedView && !count($aRows) && $bIsProfile)
        {
            $aUser = $this->getParam('aUser');
            
            $this->url()->send($aUser['user_name'], null, Phpfox::getPhrase('feed.the_activity_feed_you_are_looking_for_does_not_exist'));
        }
        
        $iTotalFeeds = (int) Phpfox::getComponentSetting($aUser['user_id'], 'feed.feed_display_limit_' . ($aUser['user_id'] !== null ? 'profile' : 'dashboard'), Phpfox::getParam('feed.feed_display_limit'));
        $this->template()->assign(array(                
                'aFeeds' => $aRows,
                'iFeedNextPage' => ($iFeedPage + 1),
                'iFeedCurrentPage' => $iFeedPage,
                'iTotalFeedPages' => 1,
                'aFeedVals' => $this->request()->getArray('val'),
                'sCustomViewType' => $sCustomViewType,
                'aFeedStatusLinks' => Phpfox::getService('follow.feed')->getShareLinks(),
                'aFeedCallback' => $aFeedCallback,
                'bIsCustomFeedView' => $bIsCustomFeedView,
                'bIsProfile' =>$bIsProfile,
                'bIsFollow' => isset($bIsFollow)? $bIsFollow: false,
                'sFeedOf' => $sFeedOf,
            )
        );
        }
        //}
        $this->template()->setHeader(array(
            'jquery/ui.js' => 'static_script',
            'follow.js' => 'module_follow',
            '<script type="text/javascript">$Core.follow.url(\'' . $this->url()->makeUrl('follow') . '\');</script>',
            '<script type="text/javascript">$Core.follow.getInfoFollow(\''.$aUser['user_id'].'\',\''.(($bIsFollow == false)? 0: 1).'\', \''.(($bIsCurrentUser == false)? 0: 1).'\');</script>'
        ));
        
        $this->template()->setHeader('cache', array(
            'jquery/plugin/jquery.highlightFade.js' => 'static_script',
            'jquery/plugin/jquery.scrollTo.js' => 'static_script',
            'quick_edit.js' => 'static_script',
            'comment.css' => 'style_css',
            'pager.css' => 'style_css',
            'feed.js' => 'module_follow',
            'friend.js' => 'module_friend',
            'friend.css' => 'style_css',
            'follow.css' => 'module_follow',
            'privacy.css' => 'module_user',
        ));  
        
        $this->template()->assign(array(
            'aUser' => $aUser,
            'sType' => 'user',
            'bIsLogin' => $bIsLogin,
            'aParentModule' => $aParentModule,
            'sKeyword' => $sKeyword,
            'bCanFollow' =>$bCanFollow,
            'bIsCurrentUser' => $bIsCurrentUser,
            'aIndex_Followers' =>isset($aIndex_Followers)?$aIndex_Followers:null,
            'aIndex_Followings' =>isset($aIndex_Followings)? $aIndex_Followings:null,
            'aIndex_Blockers' =>isset($aIndex_Blockers)? $aIndex_Blockers:null,
            'iAmountResult' => isset($iAmountResult)?$iAmountResult: null,
        ));
                
        $this->template()->setPhrase(array(
            'follow.following','follow.unfollow','follow.follow'
        ));
    }
}  
?>
